package com.zzc.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.zzc.domain.Order;
import com.zzc.domain.ProductDetail;
import com.zzc.domain.ProductImg;
import com.zzc.domain.ProductInfor;
import com.zzc.repository.*;
import com.zzc.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImp implements ProductService {

    @Autowired
    ProductInforRep productInforRep;

    @Autowired
    ProductImgRep productImgRep;

    @Autowired
    ProductDetailRep productDetailRep;

    @Autowired
    CategoryRep categoryRep;

    @Autowired
    OrderRep orderRep;

    @Override
    public String getProductInfor() {

        List<ProductInfor> productInfors = productInforRep.findAll();
        String s = JSON.toJSONString(productInfors);
        return s;
    }

    @Override
    public String getProductImg(String productName) {
        ArrayList<ProductImg> productImgs = productImgRep.findAllByProductName(productName);
        return JSON.toJSONString(productImgs);
    }

    @Override
    public String getProductDetail(String productName) {
        ArrayList<ProductDetail> productDetails = productDetailRep.finDetailByProductName(productName);
        return JSON.toJSONString(productDetails);
    }

    @Override
    public String getCategory() {
        return JSON.toJSONString(categoryRep.findAll());
    }

    @Override
    public void addOrder(String userId, String productName, String url, String unitPrice, String num, String total) {
        Order order = new Order();
        order.setNum(num);
        order.setProductName(productName);
        order.setTotal(total);
        order.setUnitPrice(unitPrice);
        order.setUrl(url);
        order.setUserId(userId);
        orderRep.save(order);
    }

    @Override
    public String getOrder(String userId) {
        return JSON.toJSONString(orderRep.findAllByUserId(userId));
    }

    @Override
    public String getProductInforByName(String name) {
        return productInforRep.findByProductName(name);
    }

    @Override
    public void deleteOrder(String userId, String name) {
        orderRep.deleteByUserIdAndName(userId,name);
    }


}
